<script setup name="BlogType">
	import {ref, reactive} from 'vue'
	import {blogTypeApi} from '@/api/type.js'
	import {
		Plus,
		Delete,
		Edit,
		Message,
		Search,
	} from '@element-plus/icons-vue'
	import {ElMessage, ElMessageBox} from "element-plus";
	
	//表格数据
	let typeList = ref([])
	const total = ref(0)
	
	//分页信息，搜索条件
	const typeQuery = reactive({
		page: 1,
		limit: 10,
		name: '',
	})
	
	const handleClick = (id) => {
		console.log('click')
		blogTypeApi.selectById(id).then(result => {
			blogType.value = result.data
		})
	}
	
	const loadData = () => {
		blogTypeApi.list(typeQuery).then(result => {
			typeList.value = result.data.list
			total.value = result.data.total
			
		})
	}
	
	loadData()
	
	const onSearch = () => {
		typeQuery.page = 1
		loadData()
	}
	
	//单个删除
	const deleteById = (id) => {
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll:false   //锁定滚动条。防止抖动
			}
		)
			.then(() => {
				blogTypeApi.deleteById(id).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//批量删除
	const ids=ref([])
	const handleSelectionChange = (rows) => {
		//console.log('多选', rows)
		ids.value = rows.map(row => row.id)
		console.log(ids.value);
	}
	const deleteAll=()=>{
		ElMessageBox.confirm(
			'确认删除吗?',
			'警告',
			{
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
				lockScroll:false   //锁定滚动条。防止抖动
			}
		)
			//点击确认删除
			.then(() => {
				ids.value = ids.value.join(',')
				blogTypeApi.deleteAll(ids.value).then(result => {
					if (result.code === 0) {
						ElMessage({message: result.msg, type: 'success',})
					} else {
						ElMessage.error(result.msg)
					}
					//刷新表格
					loadData()
				})
			})
	}
	
	//添加、编辑
	const dialogVisible=ref(false)
	const blogType=ref({})
	const title=ref()
	const showAddDialog=()=>{
		
		dialogVisible.value=true
		title.value='添加'
		blogType.value={}
	}
	
	const showUpdateDialog=(id)=>{
		dialogVisible.value=true
		title.value='编辑'
		blogType.value={}
		blogTypeApi.selectById(id).then(result => {
			blogType.value = result.data
		})
	}
	
	const addOrUpdate=()=>{
		if(blogType.value.id){  //有值，编辑
			blogTypeApi.update(blogType.value).then(result=>{
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value=false
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		}else {   //无值，添加
			blogTypeApi.add(blogType.value).then(result=>{
				if (result.code === 0) {
					ElMessage({message: result.msg, type: 'success',})
					dialogVisible.value=false
				} else {
					ElMessage.error(result.msg)
				}
				//刷新表格
				loadData()
			})
		}
	}
	
	/*import {useTokenStore} from '@/store/token.js'
	const tokenStore = useTokenStore();*/
	
	const originData = () => ({ name: "",beginUpdateTime:"",endUpdateTime:""});
	const resetForm = () => {
		Object.assign(typeQuery, originData());
	};
</script>

<template>
	<div class="back">
		<el-card class="">
			<template #header>
				<div class="header">
					<el-button type="success" @click="showAddDialog">添加</el-button>
					<el-button type="danger" @click="deleteAll">批量删除</el-button>
				</div>
			</template>
			<!--		搜索框-->
			<el-form :inline="true" :model="formInline" class="demo-form-inline">
				<el-form-item label="类别名称">
					<el-input v-model="typeQuery.name" placeholder="请输入标题" clearable/>
				</el-form-item>
				<el-form-item label="更新时间">
					<el-date-picker
						v-model="typeQuery.beginUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="开始时间"
					/>
					-
					<el-date-picker
						v-model="typeQuery.endUpdateTime"
						type="datetime"
						value-format="YYYY-MM-DD HH:mm:ss"
						placeholder="结束时间"
					/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
					<el-button @click="resetForm" round>重置</el-button>
				</el-form-item>
			</el-form>
			
			<!--		信息表格-->
			<el-table ref="multipleTableRef" :data="typeList" border stripe style="width: 100%"
					  @selection-change="handleSelectionChange" height="450px">
				<el-table-column type="selection" :selectable="selectable" width="40"/>
				<el-table-column fixed prop="id" label="ID" sortable min-width="70" align="center"/>
				<el-table-column prop="name" label="分类名称" align="center" min-width="120" show-overflow-tooltip/>
				<el-table-column prop="createTime" label="创建时间" align="center" min-width="200"/>
				<el-table-column prop="updateTime" label="更新时间" align="center" min-width="200"/>
				<el-table-column fixed="right" label="操作" min-width="150" align="center">
					<template #default="{row}">
						<el-button type="info" :icon="Message" circle @click="handleClick(row.id)"/>
						<el-button type="primary" :icon="Edit" circle @click="showUpdateDialog(row.id)"/>
						<el-button type="danger" :icon="Delete" circle @click="deleteById(row.id)"/>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				v-model:current-page="typeQuery.page"
				v-model:page-size="typeQuery.limit"
				:page-sizes="[5, 10, 15, 20]"
				:size="size"
				:disabled="disabled"
				:background="background"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
				@change="loadData"
				style="margin-top: 20px; justify-content: flex-end"
			/>
		</el-card>
		
		<!--		添加编辑弹出框-->
		<el-dialog v-model="dialogVisible" title="添加、编辑" width="500" :lock-scroll="false">
			<el-form :model="blogType">
				<el-form-item label="分类名称：" :label-width="100">
					<el-input v-model="blogType.name" autocomplete="off" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="addOrUpdate">
						确认
					</el-button>
				</div>
			</template>
		</el-dialog>
	</div>

</template>

<style scoped>

</style>
